<template>
  <div class="login">
    <div class="login_box">
      <div class="avatar_box">
        <img
          src="https://destiny001.gitee.io/admin-dist/img/logo.b5a855ee.png"
          alt=""
        />
      </div>
      <el-form ref="form" :model="form">
        <el-form-item
          prop="username"
          :rules="[
            { required: true, message: '请输入用户名',trigger: 'blur'},
            {
              pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]{5,10}$/,
              message: '用户名格式不正确',
              trigger: 'blur'
            }
          ]"
        >
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入用户名"
            size="medium"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          :rules="[
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
              pattern: /^\d{6,12}$/,
              message: '密码格式不正确',
              trigger: 'blur'
            }
          ]"
        >
          <el-input
            v-model="form.password"
            type="password"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入密码"
            size="medium"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="login-btn">
            <el-button @click="OnSubmit('form')" size="medium" type="primary"
              >登录</el-button
            >
            <el-button @click="resetForm('form')" size="medium" type="info">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getLogin } from '@/api/Login.js'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },

  created () {},
  methods: {
    OnSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await getLogin(this.form)
          console.log(res)
          this.$store.commit('initUser', res.data.data)
          if (res.data.meta.status === 400) return
          this.$router.push('/welcome')
        } else {
          alert('登陆失败')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="less">
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    width: 450px;
    height: 300px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
      }
    }
    .el-form-item {
      margin-bottom: 22px;
      height: 40px;
      .el-input__inner {
        height: 100%;
      }
    }
    .el-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
    .login-btn {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
